package com.jtjrenren.android.taxi.passenger.manger.push.cmd;


import com.library.utils.ConvertUtils;
import com.library.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/18.
 */
public class Protocl {
    public static final String MSG_ID_UPORDER = "8F00";  //上传召车信息
    public static final String MSG_ID_COMMONRESPONSE = "9002";  //回复通用应答
    public static final String MSG_ID_ORDERCANCLE = "8F09";  //订单取消
    public static final String MSG_ID_UPORDERPAYRESULT = "8F20";  //上传支付结果
    public static final String MSG_ID_HEART = "0002";  //心跳包

    public static final String MSG_ID_CENTER_RESPONSE = "9001";  //中心通用应答
    public static final String MSG_ID_CENTER_ORDER_ACCEPT = "8F01";  //中心下发接单信息
    public static final String MSG_ID_CENTER_ORDER_DISPATCH = "8F02";  //订单派发情况
    public static final String MSG_ID_CENTER_ORDER_FILTER = "8F03";  //中心通知订单正在筛选
    public static final String MSG_ID_CENTER_ORDER_DRIVER_INCOME = "8F04";  //中心通知乘客接驾中
    public static final String MSG_ID_CENTER_ORDER_AMOUNT = "8F05";  //中心下发车费信息
    public static final String MSG_ID_CENTER_ORDER_PASSGER_INCAR = "8F06";  //中心通知司机确认乘客上车
    public static final String MSG_ID_CENTER_ORDER_DRIVER_CANCLE = "8F08";  //中心下发司机取消该订单
    public static final String MSG_ID_CENTER_RESPONSE_OTHERLOGIN = "8FB1";  //其他端登陆


    public static final int RESULT_SUCCESS = 0; //成功/确认
    public static final int RESULT_FAILED = 1; //失败
    public static final int RESULT_MSG_ERROR = 2; //消息有误

    public static final String PROTOCOL_FLAG = "7E";//协议的标志位
    protected static final int ACCOUNT_SIZE = 20; //联系方式所占字节大小
    public static final int PROTOCL_SIZE_MIN = 27; //协议封装完成后最少的字节数


    /**
     * 获取校验码
     *
     * @param bytes
     * @return
     */
    protected static byte getProtoclCheckCode(byte[] bytes) {
        byte result = bytes[0];
        for (int i = 1; i < bytes.length; i++) {
            result ^= bytes[i];
        }
        return result;
    }


    /**
     * 将字符串填充0
     *
     * @param valueStr
     * @param length   字符串需要达到的长度
     * @param isRight  在字符串的前边或者后边补零 true为在后边补零
     */
    protected static String fillingZero(String valueStr, int length, boolean isRight) {
        int size = valueStr.length();
        if (size < length) {
            int dis = length - size;
            String tempZero = "";
            for (int i = 0; i < dis; i++)
                tempZero += "0";
            if (isRight)
                valueStr = valueStr + tempZero;
            else valueStr = tempZero + valueStr;
        }
        return valueStr;
    }

    /**
     * 协议转义
     *
     * @param protocolBody
     * @return
     */
    protected static String getTransferredProtocl(String protocolBody) {
        int i = 0;
        int length = protocolBody.length();
        int start, end;
        String tmp;
        StringBuilder sb = new StringBuilder();
        while (i < length) {
            start = i;
            end = i + 2;
            if (end > length - 1)
                tmp = protocolBody.substring(start);
            else
                tmp = protocolBody.substring(start, end);
            if ("7d".equalsIgnoreCase(tmp)) {
                sb.append("7d01");
            } else if ("7e".equalsIgnoreCase(tmp)) {
                sb.append("7d02");
            } else {
                sb.append(tmp);
            }
            i += 2;
        }
        return sb.toString();
    }

    /**
     * 获取接收到的协议进行转义
     *
     * @param protoclStr
     * @return
     */
    protected static String getTransReceiveProtocl(String protoclStr) {
        return protoclStr.replaceAll("7d02", "7e").replaceAll("7D02", "7e")
                .replaceAll("7d01", "7d").replaceAll("7D01", "7d");
    }


    /**
     * 对于协议流水号的封装
     *
     * @param paramInt
     * @return
     */
    protected static String getSerialNumber(int paramInt) {
        String numStr = paramInt + "";
        char[] numCharArray = numStr.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4 - numCharArray.length; i++) {
            sb.append("0");
        }
        sb.append(numStr);
        return sb.toString();
    }

    /**
     * 将联系方式封装为协议中的显示
     *
     * @param account
     * @return
     */
    protected static String getAccountEncap(String account) {
        return fillingZero(ConvertUtils.stringToAsciiHex(account), ACCOUNT_SIZE * 2, false);
    }

    /**
     * 将协议返回的account进行解析成正常的手机号
     *
     * @param accountCmdStr
     * @return
     */
    protected static String parseAccountCmdStr(String accountCmdStr) {
        String reg = "0{2,}";
        accountCmdStr = accountCmdStr.replaceAll(reg, "");
        return ConvertUtils.AsciiStringToString(accountCmdStr);
    }

    /**
     * 将数字的电召单号 转换为16进制的电召单号
     *
     * @param orderSerial
     * @return
     */
    protected static String getOrderSerialNo(String orderSerial) {
        return ConvertUtils.bytes2hex(ConvertUtils.intToByte4(Integer.parseInt(orderSerial)));
    }

    /**
     * 将协议返回的电召单号转换为int型的整数
     *
     * @param orderSerialCmdStr
     * @return
     */
    protected static String parseOrderSerialNo(String orderSerialCmdStr) {
        return Integer.parseInt(orderSerialCmdStr, 16) + "";
    }

    /**
     * 将经度或纬度转换为UINT32 转换成16进制字符串
     *
     * @param latOrlng
     * @return
     */
    public static String latLngToUINT32(String latOrlng) {
        int latOrlngInt = (int) (Double.parseDouble(latOrlng) * 600000);
        byte[] bytes = ConvertUtils.intToByte4(latOrlngInt);
        return ConvertUtils.bytes2hex(bytes);
    }

    /**
     * GBK编码，采用’\0’终结符，若无数据，则放一个0x00终结符
     *
     * @param strValue
     */
    public static String getString(String strValue) {
        String result = "00";
        if (!StringUtils.isEmpty(strValue)) {
            try {
                byte[] temp = strValue.getBytes("gbk");
                result = ConvertUtils.bytes2hex(temp) + result;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 将通过gbk编码获取的协议中的string类型转换出来
     *
     * @param cmdStringValue
     * @return
     */
    public static String cmdStringstr2String(String cmdStringValue) {
        String result = null;
        try {
            result = new String(ConvertUtils.HexStrToByte(cmdStringValue), "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 截取以00结尾的字符串
     *
     * @param str
     */
    public static List<String> end00StringToArray(String str) {
        String s, temp = "";
        List<String> strArray = new ArrayList<String>();
        for (int i = 0; i < str.length() / 2; i++) {
            s = str.substring(i * 2, i * 2 + 2);
            if (s.equals("00")) {
                strArray.add(temp);
                temp = "";
            } else {
                temp += s;
            }
        }
        return strArray;
    }
}
